package com.situ.day7;

import org.junit.Test;

import java.util.*;

public class CollectionDemo {
    @Test
    public void test1() {
        Student[] array = new Student[3];
        ArrayList<Student> list1 = new ArrayList<>();

        //有序可重复
//        ArrayList<String> list2 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        list2.add("Java");
        list2.add("UI");
        list2.add("H5");
        list2.add("H5");
        list2.add("aa");
        for (String str : list2) {
            System.out.println(str);
        }

        System.out.println("---------------");
        //无序不重复
        //无序：元素放进去的顺序和输出顺序可能是不一致
        //HashSet<String> set = new HashSet<>();
        Set<String> set = new HashSet<>();
        set.add("Java");
        set.add("UI");
        set.add("H5");
        set.add("H5");
        set.add("aa");
        for (String str : set) {
            System.out.println(str);
        }
    }

    @Test
    public void test45() {
        Map<String, String> map = new HashMap<>();
        map.put("CN", "中国");//Entry
        map.put("US", "美国");
        map.put("UK", "英国");
        //Set<Map.Entry> entrySet = map.entrySet();
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        for (Map.Entry<String, String> entry : entrySet) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println("key: " + key + ", value: " + value);
        }

        String value = map.get("CN");
        System.out.println("value: " + value);

        System.out.println("-------------");
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            String val = map.get(key);
            System.out.println("key: " + key + ", val: " + val);
        }

    }

    @Test
    public void test33() {
        Map<String, String> map = new HashMap<>();
        map.put("CN", "中国");
        map.put("US", "美国");
        map.put("UK", "英国");
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {//hasNext()判断有没有下一个
            Map.Entry<String, String> entry = iterator.next();
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println("key: " + key + ", value: " + value);
        }
        System.out.println("-----------------");
        Set<String> keySet = map.keySet();
        Iterator<String> iter = keySet.iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            String value = map.get(key);
            System.out.println("key: " + key + ", value: " + value);
        }
    }
}
